﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net.Configuration;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;
using SocketCore;
using SocketCore.Network;
using Message=SocketCore.Network.Message;
using System.Net;

namespace SocketClientTest
{
    public partial class Form1 : Form
    {
        private SocketClient m_sClient = new SocketClient();
        private System.Net.NetworkInformation.Ping m_Ping;
        public Form1()
        {
            
            InitializeComponent();
            m_sClient.Connected += new SocketClient.ConnectHandler(m_sClient_Connected);
            m_sClient.Received += new SocketClient.ReceiveHandler(m_sClient_Received);
            m_sClient.Disconnected += new SocketClient.DisconnectHandler(m_sClient_Disconnected);
            m_Ping = new Ping();
            tbIp.Text = "10.254.60.66";
            tbPort.Text = "22110";

            
            System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += new System.Net.NetworkInformation.NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += new System.Net.NetworkInformation.NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
        }

        void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e)
        {
            
            if (e.IsAvailable)
            {
                MessageBox.Show("Network connected");
            }
            else
                MessageBox.Show("Network Disconnected");
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
           
        }

        void m_sClient_Disconnected(object sender)
        {
            MessageBox.Show("Disconnect >_<");
        }

        void m_sClient_Received(object sender, Message msg)
        {
            MessageBox.Show(msg.Data.ToString());
        }

        void m_sClient_Connected(object sender, bool bSuccess)
        {
            if(bSuccess)
                MessageBox.Show("conected");
            else
                MessageBox.Show("can not conect");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sIP = tbIp.Text;
            string sPort = tbPort.Text;
            int iPort = Convert.ToInt32(sPort);

            m_sClient.Connect(sIP, iPort);

           // System.Net.Configuration.SocketElement ss = new SocketElement();
           
            

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string str = tbData.Text;
            SocketCore.Network.Message mess  = new Message(PackageTypeEnum.Ascii,false,(object)str);
            if(m_sClient.IsConnected)
                m_sClient.SendMessage(mess);
            
        }
    }
}
